package output;

import java.util.List;

import forum.ForumItem;
import forum.ForumPost;
import forum.ForumSentence;
import forum.ForumTag;
import forum.ForumTopic;

public abstract class DatabaseOutput implements Output {

	public abstract void insertSentence(ForumSentence sent) throws Exception;

	public abstract void insertSentenceItem(ForumSentence sent, ForumItem item)
			throws Exception;

	public abstract void insertPost(ForumPost post) throws Exception;

	public abstract void insertItem(ForumItem item) throws Exception;

	public abstract void insertTopic(ForumTopic topic) throws Exception;

	public abstract void insertTag(ForumTag tag) throws Exception;

	public abstract void insertTagsInPost(ForumPost post, ForumTag tag)
			throws Exception;

	@Override
	public void outputThread(List<ForumPost> f) throws Exception {
		for (ForumPost p : f) {

			insertPost(p);

			for (ForumTag tag : p.getForumTags()) {
				if (!tag.isPrinted()) {
					insertTag(tag);
					tag.setPrinted(true);
				}
				insertTagsInPost(p, tag);
			}

			for (ForumSentence s : p.getSentences()) {
				insertSentence(s);

				for (ForumItem i : s.getItems()) {
					if (!i.getTopic().isPrinted()) {
						insertTopic(i.getTopic());
						i.getTopic().setPrinted(true);
					}
					if (!i.isPrinted()) {
						insertItem(i);
						i.setPrinted(true);
					}
					insertSentenceItem(s, i);
				}
			}
		}
	}
}
